fix: ArrayExpression - immediate index access#2409
Conversation
|
pkg.pr.new packages benchmark commit |
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:
📋 All resultsClick to reveal the results table (350 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
Resolution Time Benchmark---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.84, 1.69, 3.56, 5.16, 6.19, 9.44, 18.42, 20.38]
line [0.75, 1.51, 3.59, 5.18, 6.45, 8.34, 17.55, 18.78]
line [0.85, 1.69, 3.57, 5.49, 6.29, 9.64, 19.76, 21.32]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.25, 0.44, 0.59, 0.76, 0.94, 1.06, 1.24, 1.38]
line [0.31, 0.49, 0.54, 0.72, 0.96, 1.01, 1.26, 1.29]
line [0.29, 0.48, 0.60, 0.79, 1.03, 1.06, 1.23, 1.40]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.75, 1.65, 3.41, 5.45, 10.51, 21.32, 47.01, 97.34]
line [0.76, 1.69, 3.65, 5.46, 10.49, 21.77, 46.82, 95.85]
line [0.96, 1.78, 3.60, 5.33, 10.45, 22.81, 47.16, 98.28]
|
There was a problem hiding this comment.
Pull request overview
Fixes indexing into TGSL array literals by special-casing ArrayExpression targets so immediate index access can resolve correctly.
Changes:
- Added
ArrayExpressionhandling inaccessIndexto return a direct element when the index is comptime-known. - Added tests covering array-literal indexing with comptime-known and runtime-known indices.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/typegpu/src/tgsl/accessIndex.ts | Adds an ArrayExpression fast-path for constant indices to return the selected element snippet directly. |
| packages/typegpu/tests/array.test.ts | Adds snapshots validating codegen for [...][0/1] and arr[i] patterns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| it('array expressions can be indexed into with a comptime-known index', () => { | ||
| function foo() { | ||
| 'use gpu'; | ||
| const i = 2; | ||
| const a = [i, 2][0]; | ||
| const b = [i, 2][1]; | ||
| } |
There was a problem hiding this comment.
The new tests cover constant-vs-variable indices, but they don’t exercise an important edge case for this change: immediate indexing into an array expression that would otherwise be invalid to construct (e.g., containing a non-ephemeral ref/argument ref). Adding a regression test like const v = d.vec2f(...); const x = [d.vec2f(...), v][0]; (and/or selecting the ref element) would better protect this behavior from regressions.
77d3a32 to
de46a9a
Compare
de46a9a to
ea911f3
Compare
There was a problem hiding this comment.
It wasn't immediately obvious to me what would happen in these two cases, so here are two free tests
it('resolves array expression elements when accessed with comptime-known index', () => {
let n = 0;
const next = tgpu.comptime(() => n++);
function foo() {
'use gpu';
const a = [next(), next()][0];
const b = [next(), next()][1];
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
const a = 0;
const b = 3;
}"
`);
});
it('prunes definitions in array expressions accessed with comptime-known index', ({ root }) => {
const u1 = root.createUniform(d.u32, 1);
const u2 = root.createUniform(d.u32, 2);
const u3 = root.createUniform(d.u32, 3);
const u4 = root.createUniform(d.u32, 4);
function foo() {
'use gpu';
const a = [u1.$, u2.$][0];
const b = [u3.$, u4.$][1];
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<uniform> u1: u32;
@group(0) @binding(1) var<uniform> u4: u32;
fn foo() {
let a = u1;
let b = u4;
}"
`);
});ea911f3 to
d89b53d
Compare
d89b53d to
70b28c7
Compare
No description provided.